package com.itbaizhan.openhis.controller.erp;

import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.domain.Purchase;
import com.itbaizhan.openhis.domain.PurchaseItem;
import com.itbaizhan.openhis.dto.PurchaseDto;
import com.itbaizhan.openhis.dto.PurchaseFormDto;
import com.itbaizhan.openhis.service.PurchaseService;
import com.itbaizhan.openhis.utils.IdGeneratorSnowflake;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

//采购入库，分为主表 与 详细表
@RestController
@RequestMapping("erp/purchase")
public class PurchaseController {

    @Reference
    private PurchaseService purchaseService;

    /**
     * 分页查询采购入库
     * @param purchaseDto
     * @return
     */
    @GetMapping("listPurchaseForPage")
    public AjaxResult listPurchaseForPage(PurchaseDto purchaseDto){
        DataGridView dataGridView = purchaseService.listPurchaseForPage(purchaseDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 分页查询待审核的单据
     * @param purchaseDto
     * @return
     */
    @GetMapping("listPurchasePendingForPage")
    public AjaxResult listPurchasePendingForPage(PurchaseDto purchaseDto){
        //2是待审核状态，增加2的状态查询条件，再调用的查询方法
        purchaseDto.setStatus(Constants.STOCK_PURCHASE_STATUS_2);
        DataGridView dataGridView = purchaseService.listPurchaseForPage(purchaseDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 提交审核
     * @param purchaseId
     * @return
     */
    @PostMapping("doAudit/{purchaseId}")
    public AjaxResult doAudit(@PathVariable String purchaseId){
        //先要查出采购单，再判定状态是未提交审核和审核失败才可心提交
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        //判断状态为1未提交，4审核失败
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1) || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)){
            //提交审核
            return AjaxResult.toAjax(purchaseService.doAudit(purchaseId, ShiroSecurityUtils.getCurrentSimpleUser()));
        }else {
            return AjaxResult.fail("当前单据状态不是未提交或审核失败，不能提交");
        }

    }

    /**
     * 审核通过
     * @param purchaseId
     * @return
     */
    @PostMapping("auditPass/{purchaseId}")
    public AjaxResult auduitPass(@PathVariable String purchaseId){
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_2)){
            int i = purchaseService.auditPass(purchaseId);
            return AjaxResult.toAjax(i);
        }else {
            return AjaxResult.fail("当前单据状态不是【待审核】，不能审核");
        }
    }

    /**
     * 审核不通过
     * @param purchaseId
     * @param auditMsg
     * @return
     */

    @PostMapping("auditNoPass/{purchaseId}/{auditMsg}")
    public AjaxResult auduitNoPass(@PathVariable String purchaseId,@PathVariable String auditMsg){
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_2)){
            int i = purchaseService.auditNoPass(purchaseId,auditMsg);
            return AjaxResult.toAjax(i);
        }else {
            return AjaxResult.fail("当前单据状态不是【待审核】，不能审核");
        }
    }

    /**
     * 根据id查询采购详情
     * @param purchaseId
     * @return
     */
    @GetMapping("getPurchaseItemById/{purchaseId}")
    public AjaxResult getPurchaseItemById(@PathVariable String purchaseId){
        List<PurchaseItem> items = purchaseService.getPurchaseItemById(purchaseId);
        return AjaxResult.success(items);
    }

    /**
     * 提交入库
     * @param purchaseId
     * @return
     */
    @PostMapping("doInventory/{purchaseId}")
    public AjaxResult doInventory(@PathVariable String purchaseId){
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        //判断状态是否审核通过
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_3)){
            return AjaxResult.toAjax(purchaseService.doInventory(purchaseId,ShiroSecurityUtils.getCurrentSimpleUser()));
        }else if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_6)){
            return AjaxResult.fail("采购单【" + purchaseId + "】已入库，不能重复入库");
        }else {
            return AjaxResult.fail("采购单【" + purchaseId + "】没有审核通过，不能入库");
        }
    }


    /**
     * 提交作废
     * @param purchaseId
     * @return
     */
    @PostMapping("doInvalid/{purchaseId}")
    public AjaxResult doInvalid(@PathVariable String purchaseId){
        //先要查出采购单，再判定状态是未提交审核和审核失败才可以提交
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        //判断状态为1未提交，4审核失败
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1) || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)){
            //提交审核
            return AjaxResult.toAjax(purchaseService.doInvalid(purchaseId, ShiroSecurityUtils.getCurrentSimpleUser()));
        }else {
            return AjaxResult.fail("当前单据状态不是未提交或审核失败，不能作废");
        }

    }

    /**
     * 生成采购id
     * @return
     */
    @GetMapping("generatePurchaseId")
    public AjaxResult generatePurchaseId(){
        return AjaxResult.success(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PROFIX_CG));

    }

    /**
     * 暂存采购单
     * @param purchaseFormDto
     * @return
     */
    @PostMapping("addPurchase")
    public AjaxResult addPurchase(@RequestBody PurchaseFormDto purchaseFormDto){
        //判断是否未提交审核和审核失败才可以提交,如果是false就不能提交
        if(!checkPurchase(purchaseFormDto)){
            return AjaxResult.fail("当前单据状态不是未提交或审核失败，不能提交");
        }
        purchaseFormDto.getPurchaseDto().setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(purchaseService.addPurchaseAndItem(purchaseFormDto));
    }

    /**
     * 保存并提交审核
     * @param purchaseFormDto
     * @return
     */
    @PostMapping("addPurchaseToAudit")
    public AjaxResult addPurchaseToAudit(@RequestBody PurchaseFormDto purchaseFormDto){
        //有两个表要添加，主表数据和采购单详情

        //判断是否未提交审核和审核失败才可以提交,如果是false就不能提交
        if(!checkPurchase(purchaseFormDto)){
            return AjaxResult.fail("当前单据状态不是未提交或审核失败，不能提交");
        }
        //提交人
        purchaseFormDto.getPurchaseDto().setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(purchaseService.addPurchaseToAudit(purchaseFormDto));
    }

    private boolean checkPurchase(PurchaseFormDto purchaseFormDto) {
        String purchaseId = purchaseFormDto.getPurchaseDto().getPurchaseId();
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        if (purchase == null) {
            return true;  //数据库没有这数据，可以直接添加
        }
        //判断状态为1未提交，4审核失败
        if (purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1) || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)) {
            return true;
        }
        return false;
    }

    /**
     * 根据采购单id查询采购单详情
     * @param purchaseId
     * @return
     */
    @GetMapping("queryPurchaseAndItemByPurchaseId/{purchaseId}")
    public AjaxResult queryPurchaseAndItemByPurchaseId(@PathVariable String purchaseId){
        Purchase purchase = purchaseService.getPurchaseById(purchaseId);
        if(purchase ==null){
            return AjaxResult.fail("单据号【"+ purchaseId + "】不存在");
        }else {
            List<PurchaseItem> items=purchaseService.getPurchaseItemById(purchaseId);
            //把数据存入hashMap中，再传给前端
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("purchase",purchase);
            hashMap.put("items",items);
            return AjaxResult.success(hashMap);
        }
    }
}
